package com.ruoyi.yipaiju.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.ApiResult;
import com.ruoyi.common.core.domain.entity.XcxUser;
import com.ruoyi.common.utils.CurrencyUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.yipaiju.domain.HousePreuploadOrder;
import com.ruoyi.yipaiju.domain.HousePreuploadOrderFollower;
import com.ruoyi.yipaiju.domain.HouseUploadingOrder;
import com.ruoyi.yipaiju.service.IHousePreuploadOrderFollowerService;
import com.ruoyi.yipaiju.service.IHousePreuploadOrderService;
import com.ruoyi.yipaiju.service.IHouseUploadingOrderService;
import com.ruoyi.yipaiju.service.IXcxUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 放盘
 */
@RestController
@RequestMapping("/houses")
public class HousesPreuploadController extends BaseController {

    /**
     * type==1?'优选':'折扣'
     * 优选房源  == 法拍房
     */
    @Autowired
    private IHousePreuploadOrderService housePreuploadOrderService;
    @Autowired
    private IHousePreuploadOrderFollowerService housePreuploadOrderFollowerService;
    @Autowired
    private IXcxUserService xcxUserService;

    /**
     * 放盘订单
     * @param housePreuploadOrder
     * @return
     */
    @PostMapping("/preupload/orders")
    public ApiResult preuploadOrders(@RequestBody HousePreuploadOrder housePreuploadOrder){
        housePreuploadOrder.setNo(CurrencyUtil.create(10));
        housePreuploadOrder.setClient(SecurityUtils.getUserId());
        housePreuploadOrderService.insertHousePreuploadOrder(housePreuploadOrder);
        return ApiResult.success();
    }

    /**
     * 放盘订单详情
     * @param id
     * @return
     */
    @GetMapping("/preupload/orders/{id}")
    public ApiResult preuploadOrdersInfo(@PathVariable("id") Long id){
        HousePreuploadOrder housePreuploadOrder = housePreuploadOrderService.selectHousePreuploadOrderById(id);

        List<HousePreuploadOrderFollower> housePreuploadOrderFollowers = housePreuploadOrderFollowerService.selectHousePreuploadOrderFollowerList(
                Builder.of(HousePreuploadOrderFollower::new).with(HousePreuploadOrderFollower::setHousePreuploadOrderId, housePreuploadOrder.getId()).build());
        housePreuploadOrder.setHousePreuploadOrderFollowers(housePreuploadOrderFollowers);

        List<Long> ids = housePreuploadOrderFollowers.stream().map(item -> Long.valueOf(item.getMarketer())).distinct().collect(Collectors.toList());
        Map<Long, XcxUser> xcxUserMap = xcxUserService.getXcxUserByIds(ids);
        housePreuploadOrderFollowers.stream().forEach(item -> {
            XcxUser xcxUser = xcxUserMap.get(Long.valueOf(item.getMarketer()));
            if (xcxUser != null){
                item.setImg(xcxUser.getAvatarUrl());
                item.setMarkName(xcxUser.getNickname());
                item.setTelphone(xcxUser.getPhone());
            }
        });
        return ApiResult.success(housePreuploadOrder);
    }

    /**
     * 放盘跟单
     * @param housePreuploadOrderFollower
     * @return
     */
    @PostMapping("/preupload/orders/followers")
    public ApiResult preuploadOrdersFollowers(@RequestBody HousePreuploadOrderFollower housePreuploadOrderFollower){
        if (StringUtils.isEmpty(housePreuploadOrderFollower.getMarketer())){
            housePreuploadOrderFollower.setMarketer(SecurityUtils.getUserId().toString());
        }
        HousePreuploadOrderFollower housePreuploadOrderFollower1 = housePreuploadOrderFollowerService.selectOneHousePreuploadOrderFollower(housePreuploadOrderFollower);

        if (housePreuploadOrderFollower1 == null){
            housePreuploadOrderFollower1 = Builder.of(HousePreuploadOrderFollower::new)
                    .with(HousePreuploadOrderFollower::setHousePreuploadOrderId, housePreuploadOrderFollower.getHousePreuploadOrderId())
                    .with(HousePreuploadOrderFollower::setMarketer, housePreuploadOrderFollower.getMarketer())
                    .with(HousePreuploadOrderFollower::setStage, 1)
                    .build();
            housePreuploadOrderFollowerService.insertHousePreuploadOrderFollower(housePreuploadOrderFollower1);
            HousePreuploadOrder housePreuploadOrder = housePreuploadOrderService.selectHousePreuploadOrderById(housePreuploadOrderFollower.getHousePreuploadOrderId());
            housePreuploadOrder.setStage(2);
            housePreuploadOrder.setServingTime(new Date());
            housePreuploadOrderService.updateHousePreuploadOrder(housePreuploadOrder);
        }
        HousePreuploadOrder housePreuploadOrder = housePreuploadOrderService.selectHousePreuploadOrderById(housePreuploadOrderFollower.getHousePreuploadOrderId());
        if (housePreuploadOrder != null) {
            housePreuploadOrderFollower1.setHousePreuploadOrder(housePreuploadOrder);
            return ApiResult.success(housePreuploadOrderFollower1);
        }
        return ApiResult.error();
    }

    /**
     * 设置备注和下一步状态更新
     * @param id
     * @param housePreuploadOrderFollower
     * @return
     */
    @PostMapping("/preupload/orders/followers/{id}")
    public ApiResult preuploadOrdersFollowersInfo(@PathVariable("id") Long id, @RequestBody HousePreuploadOrderFollower housePreuploadOrderFollower){
        HousePreuploadOrderFollower housePreuploadOrderFollower1 = housePreuploadOrderFollowerService.selectHousePreuploadOrderFollowerById(id);
        if (housePreuploadOrderFollower1 != null){
            if (housePreuploadOrderFollower.getRemark() != null){
                housePreuploadOrderFollower1.setRemark(housePreuploadOrderFollower.getRemark());
            }
            if (housePreuploadOrderFollower.getStage() != null){
                housePreuploadOrderFollower1.setStage(housePreuploadOrderFollower.getStage());
                if (housePreuploadOrderFollower.getStage().intValue() == 2){
                    housePreuploadOrderFollower1.setGetDemandTime(new Date());
                }
                if (housePreuploadOrderFollower.getStage().intValue() == 3){
                    housePreuploadOrderFollower1.setOlInviteTime(new Date());
                }

                if (housePreuploadOrderFollower.getStage().intValue() == 4){
                    housePreuploadOrderFollower1.setInterviewTime(new Date());
                }
                if (housePreuploadOrderFollower.getStage().intValue() == 5){
                    housePreuploadOrderFollower1.setFinishTime(new Date());
                    HousePreuploadOrder housePreuploadOrder = housePreuploadOrderService.selectHousePreuploadOrderById(housePreuploadOrderFollower1.getHousePreuploadOrderId());
                    housePreuploadOrder.setStage(3);
                    housePreuploadOrder.setFinishTime(new Date());
                    housePreuploadOrder.setFinished(1);
                    housePreuploadOrderService.updateHousePreuploadOrder(housePreuploadOrder);
                }
            }
            housePreuploadOrderFollowerService.updateHousePreuploadOrderFollower(housePreuploadOrderFollower1);
            HousePreuploadOrder housePreuploadOrder = housePreuploadOrderService.selectHousePreuploadOrderById(housePreuploadOrderFollower1.getHousePreuploadOrderId());
            housePreuploadOrderFollower1.setHousePreuploadOrder(housePreuploadOrder);
            return ApiResult.success(housePreuploadOrderFollower1);
        }
        return ApiResult.error();
    }

}
